<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Activity;
use app\index\model\Users;
/*活动模块*/
class Index extends Common
{  

    public function index(){
      //echo 11;
      return $this->fetch('Index/index');
    }
    /*
     轮播图接口
     */
    public function carousel()
    {
        
         $result=Activity::banner();
         if($result){
            return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);
         }
       
    }
    //活动接口
    public function activity(){
       $cityName=input('cityName');//城市名称
       $classifyId=(int)input('classifyId');//活动分类id
       $sortId=(int)input('sortId');//排序 1 默认排序 2领队人气最高 3领队好评优先 4离我较近
       $uid=(int)input('uid');// 用户id
       $nowtime=time();//现在时间戳
       $page=input('page');//页码值
       if(empty($page)){
         return json(['state'=>'206','msg'=>'页码为空']);
       }
       if(!empty($sortId) && $sortId>4){
        return json(['start'=>'207','msg'=>'排序方式参数有误']);
       }
       $limit=5;
       $start=($page-1)*$limit;
       $where="createTime>$nowtime";
       if(!empty($cityName)){
           $where=$where." and cityName='$cityName'";
         }
       if(!empty($classifyId)){
         $where=$where." and cid='$classifyId'";
        }
        if($sortId==1){
              $result = Activity::sel($where,$start,$limit,$uid);
         }
         if($sortId==2){
           $result = Activity::popularity($where,$start,$limit); 
         }
         if($sortId==3){
           $result = Activity::evaluate($where,$start,$limit); 
         }
         
         if(empty($classifyId) && empty($sortId)){
           $result = Activity::sel($where,$start,$limit,$uid);
         }
         if(!empty($classifyId) && empty($sortId)){
           $result = Activity::sel($where,$start,$limit,$uid);
         }
       if(!empty($uid)){
         $user=Db::name('user_info')->field('latitude,longitude')->where(['uid'=>$uid,'userFlag'=>'1'])->find();
         if(empty($user)){
           return json(['state'=>'205','msg'=>'用户已不存在']);
         }
        if($sortId==4){
          $lat=$user['latitude'];//纬度
          $lng=$user['longitude'];//经度    
          $result = Activity::sel($where,$start,$limit,$uid); 
          if(empty($result)){
             return json(['state'=>'201','msg'=>'查询内容不存在']);
          }
          //根据经纬度转化成距离
         foreach ($result as  $key=>$val) {
         $result[$key]['list'] = $this->getDistance($lng, $lat, $val['lng'], $val['lat'], 2);
          //判断用户的距离范围
          }
         if(empty($result)){
          return json(['state'=>'201','msg'=>'查询内容不存在']);
         } 
         //循环遍历
         foreach($result as $k => $v){
            $sort['list'][$k] = $v['list'];
         }
           //先按距离升序排序
           array_multisort($sort['list'],SORT_ASC,$result);
         }
          if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
           }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);
          }
       }else{
        if($sortId==4){
             $result = Activity::sel($where,$start,$limit,$uid); 
           }
            if($result){
               return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
             }else{
              return json(['state'=>'201','msg'=>'查询内容不存在']);
         }
       }
        
    }
    //活动详情
    public function details(){
       $uid=Request::instance()->post('uid','');//用户id 
       $activity_id=Request::instance()->post('activity_id','');//活动id 
      if(empty($uid)){
        return json(['state'=>'202','msg'=>'用户为空']);
      }
      if(empty($activity_id)){
         return json(['state'=>'203','msg'=>'参数不足']);
      }
       
       $result = Activity::actDetails($activity_id,$uid);
       if($result){
         $result['upTo_time1']=strtotime($result['upTo_time']);
         $result['activityDesc']=htmlspecialchars_decode(htmlspecialchars_decode($result['activityDesc']));
         //print_r($activityDesc);die;
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       }
    }
    //活动玩伴列表
    public function playmate(){
       $uid=Request::instance()->post('uid','');//用户id 
       $activity_id=Request::instance()->post('activity_id','');//活动id 
       if(empty($activity_id)){
         return json(['state'=>'203','msg'=>'参数不足']);
       }
      if(!empty($uid)){
       $userlist=Users::selOne($uid);
       if(empty($userlist)){
        return json(['state'=>'203','msg'=>'参数不存在']);
       }
        $tids=Db::name('activity')->where(['id'=>$activity_id])->field('tid')->find();
        if(empty($tids)){
           return json(['state'=>'203','msg'=>'参数不存在']);
        }
        //查询我有没有购买该活动
       $actState=Db::name('activity_users')->where(['uids'=>$uid,'activity_id'=>$activity_id])->find();
       if($actState){
         $actState='1';
       }else{
        $actState='-1';
       }
       $lat=$userlist['latitude'];//纬度
       $lng=$userlist['longitude'];//经度
       //查询
       $result = Activity::actPlay($activity_id,$uid);
    
       if(empty($result)){
           return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','actState'=>$actState,'tid'=>$tids['tid'],'result'=>$result]));
       }
       //根据经纬度转化成距离
       foreach ($result as  $key=>$val) {
         $result[$key]['list'] = $this->getDistance($lng, $lat, $val['longitude'], $val['latitude'], 2);
         if($result[$key]['list']>999){
              $result[$key]['list']=999;
             }
          }
         //循环遍历
         foreach($result as $k => $v){
            $sort['list'][$k] = $v['list'];
         }
         //先按距离升序排序
         array_multisort($sort['list'],SORT_ASC,$result);
      }else{
         $result = Activity::actPlaymate($activity_id);
      }
       if($result){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','actState'=>$actState,'tid'=>$tids['tid'],'result'=>$result]));
       }else{
        return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','actState'=>$actState,'tid'=>$tids['tid'],'result'=>$result]));
       }
    }
    //领队接口
    public function captain(){
       $uid=Request::instance()->post('uid','');//用户id 
       $captain_id=Request::instance()->post('captain_id','');//领队id 
      if(empty($captain_id)){
         return json(['state'=>'203','msg'=>'参数不足']);
      }
      $result = Activity::actCaptain($captain_id);
      if(empty($result)){
        return json(['state'=>'201','msg'=>'查询内容不存在']);
      }
      if(!empty($uid)){
        $re=Db::name('user_captain')->where(['uid'=>$uid,'captain_id'=>$result['captain_id']])->find();
        if($re){
          $result['stateId']='1';
        }else{
          $result['stateId']='-1';
        }
      }
       if($result){
        $result['chatId']=1;
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       }
    }
    //领队发布的活动
    public function publish(){
      $captain_id=Request::instance()->post('captain_id','');//领队id 
      $page=input('page');//当前页面值
      if(empty($captain_id) || empty($page)){
        return json(['state'=>'202','msg'=>'参数缺失']);
      }
       $limit =3;
       $start = ($page-1)*$limit;
       $re=Activity::actRelease($captain_id,$start,$limit);
       if($re){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$re]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       } 
    }
    //领队发布的活动评价
    public function captainAise(){
      $captain_id=Request::instance()->post('captain_id','');//领队id 
      $page=input('page');//当前页面值
      if(empty($captain_id) || empty($page)){
        return json(['state'=>'202','msg'=>'参数缺失']);
      }
       $limit =3;
       $start = ($page-1)*$limit;
       $re=Activity::appraise($captain_id,$start,$limit);
       if($re){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$re]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       } 
    }
    //活动详情的活动特色接口
    public function activityDesc(){
      $activity_id=input('activity_id');//活动特色
      if(empty($activity_id)){
        return json(['state'=>'205','msg'=>'参数为空']);
      }
      $activity=Db::name('activity')->where(['id'=>$activity_id])->field('activityDesc')->find();
      $activityDetail=htmlspecialchars_decode($activity['activityDesc']);
       exit(json_encode($activityDetail));die;
        $this->assign('activityDetail',$activityDetail);
         return $this->fetch('Index/activityDesc');
    }
        
}
